package juntos.lbcv.utils;

import java.io.IOException;

import juntos.lbcv.constant.TypeMarque;
import juntos.lbcv.dao.InstallRefDAO;
import juntos.lbcv.model.RefDepartement;
import juntos.lbcv.model.RefMarque;
import juntos.lbcv.model.RefRegion;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("installRefTable")
public class InstallRefTable  {

	private static String addrForGetDepAndReg = "http://www.villorama.com";
	private static String urlForMarque = Lbc.leBonCoinAdr2+"ajax/brand_model_list.html";
	
	@Autowired
	InstallRefDAO installRefDAO;
	
	private static Logger log = Logger.getLogger(InstallRefTable.class);

	
	@Transactional
	public void insertRegionDepartementFromSiteWeb() {

		try {
			Document doc = Jsoup.connect(addrForGetDepAndReg+"/france/regions.html").get();
			Elements listRegion = doc.select("div[id=content]").select(
					"ul").select("li").select("a");
			
			int nbRegion = listRegion.size();
			for (int j = 0; j < nbRegion; j++) {
				Element a = listRegion.get(j);
				String linkDep = a.attr("href");
				RefRegion region = installRefDAO.persistRegion(a.text());
				insertDepartementFromSiteWeb(region,linkDep);
			}
		} catch (IOException e) {
			log.debug(e.getMessage());
		}
	}
	
	private void insertDepartementFromSiteWeb(RefRegion region, String linkDep) {

		try {
			Document doc = Jsoup.connect(
					addrForGetDepAndReg + linkDep + "departements.html").get();
			Elements listDept = doc.select("div[id=content]").select("ul")
					.select("li").select("a");

			int nbDept = listDept.size();
			for (int j = 0; j < nbDept; j++) {
				Element a = listDept.get(j);
				installRefDAO.persistDepartement(region, a.text());
			}
		} catch (IOException e) {
			log.debug(e.getMessage());
		}
	}
	

	@Transactional
	public void insertMarqueModeleFromLeBonCoin() {

		try {
			Document doc = Jsoup.connect(urlForMarque).get();
			Elements listMarques = doc.select("optgroup");
		
			for (int i = 0; i < 2; i++) {
				// i = 0 pour marques courantes;
				// i= 1 pour autres marques;
				Elements marques = listMarques.get(i).select("option");
				int nbMarque = marques.size();
				for (int j = 0; j < nbMarque; j++) {
					String marqueLabel = marques.get(j).attr("value");
					RefMarque marque = installRefDAO.persistMarque(
							TypeMarque.values()[i], marqueLabel);
					insertModeleFromLeBonCoin(marque);
				}
			}
		} catch (IOException e) {
			log.debug(e.getMessage());
		}
	}
	
	private void insertModeleFromLeBonCoin(RefMarque marque) {

		try {
			Document doc = Jsoup.connect(urlForMarque+"?brand="+marque.getLabel().replace(" ", "_")).get();
			Elements listModeles = doc.select("option");

			int nbModeles = listModeles.size();
			for (int j = 0; j < nbModeles; j++) {
				String modeleLabel = listModeles.get(j).attr("value");
				installRefDAO.persistModele(marque, modeleLabel);
			}
		} catch (IOException e) {
			log.debug(e.getMessage());
		}
	}
	
}
